﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LiveSequence.Common.Tree;
using LiveSequence.Suite;
using LiveSequence.Suite.Commands;
using LiveSequence.Suite.Documents;
using LiveSequence.Suite.Helpers;
using LiveSequence.Suite.Interfaces;
using LiveSequence.Common.Domain;

namespace LiveSequence.Suite.Domain
{
  public class CreateDiagramCommand : DocumentCommand<SeqDiagramDoc>
  {
    public CreateDiagramCommand(SeqDiagramDoc document)
      : base(document)
    {

    }

    #region INodeCommand Members


    public override string Text(object arg)
    {
      return "Add Diagram";
    }
    public override bool Execute(object arg)
    {
      MethodCallInfo methodCallInfo = arg as MethodCallInfo;

      ProjectDoc projectDoc = DocManager.Instance.MasterDocument;

      SequenceData sequenceData = AssemblyExplorer.Instance.GetSequenceData(methodCallInfo.MethodCallName,
                                                                           methodCallInfo.MethodCallType,
                                                                           methodCallInfo.MethodCallNamespace,
                                                                           methodCallInfo.TargetMethodAssembly);

      SequenceData copySequenceData = sequenceData.Clone() as SequenceData;

      LSMethodCallDiagram diagram = new LSMethodCallDiagram(methodCallInfo.MethodCallName,
                                 methodCallInfo.MethodCallType,
                                 methodCallInfo.MethodCallNamespace,
                                 methodCallInfo.TargetMethodAssembly, copySequenceData);

      SeqDiagramDoc seqDiagramDoc = new SeqDiagramDoc(diagram);

      if (projectDoc != null)
      {
        projectDoc.AddDiagram(seqDiagramDoc);
      }

      DocManager.Instance.ActiveDocument = seqDiagramDoc;
      return true;
    }

    public override bool CanExecute(object arg)
    {
      return true;
    }



    #endregion
  }
}
